/*
 * @Author: wuchenge
 * @Date:   2020-07-06 14:16:57
 * @Last Modified by:   wuchenge
 * @Last Modified time: 2020-12-07 21:46:45
 */

export default {
    data() {
        return {
            // 当前url
            preUrl: "",
            // 返回的要存储存的数组
            template_list: [],
            // 搜索条件
            filterSearch: {},
            // 分页
            page: {
                current: 1,
                size: 10,
                total: 0
            },
            // 多选
            multipleSelection: [],
            // 加载...
            listLoading: false,
            // 弹框
            ifModify: false,
            // 表单
            form: {},
        }
    },
    filters: {
        numToString(value) {
            return value.toString()
        }
    },
    created() {
        this.getList()
    },
    methods: {
        showLoading() {
            this.listLoading = true
        },
        hideLoading() {
            this.listLoading = false
        },
        // 处理列表结果
        getListResult(data) {
            this.template_list = data.list
        },
        // 获取请求列表分页url
        getListUrl() {
            return `${this.preUrl}`
        },
        // 获取列表
        getList() {
            if (this.preUrl == '') return
            this.showLoading()
            let url = this.getListUrl()

            let tmp = {
                'page': this.page.current,
                'size': this.page.size
            }
            let objs = Object.assign(tmp, this.filterSearch)
            this.$ajax.post(url, objs)
                .then(res => {
                    let data = res.data
                    this.page.total = data.total
                    this.getListResult(data)
                    this.hideLoading()
                })
                .catch(err => {
                    this.hideLoading()
                })
        },
        // 批量删除url
        addOrEditUrl() {
            return this.preUrl + '/modify'
        },
        // 添加/编辑
        addOrEdit(data, callback = false) {
            // 添加验证
            if (typeof(callback) === 'function') {
                let flag = callback()
                if (!flag) {
                    return false
                }
            }
            this.showLoading()
            let msg = data.id ? '修改' : '增加'
            let url = this.addOrEditUrl()
            this.$ajax.post(url, data)
                .then(res => {
                    if (!res.error_code) {
                        this.$Message.success(msg + res.message)
                        this.getList()
                        if (this.getOptions) this.getOptions()
                    } else {
                        this.$Message.error(res.message)
                    }

                    this.hideLoading()
                }).catch(err => {
                    this.hideLoading()
                })
        },
        // 批量删除url
        showOrHideUrl() {
            return this.preUrl + '/status'
        },
        showOrHide(id, status) {
            this.$Modal.confirm({
                title: '是否改变状态',
                onOk: () => {
                    let data = {
                        id,
                        status
                    }
                    let url = this.showOrHideUrl()

                    this.$ajax.post(url, data)
                        .then(res => {
                            let data = res.data
                            if (!data.error_code) {
                                this.$Message.success(data.message)
                                this.getList()
                                if (this.getOptions) this.getOptions()
                            } else {
                                this.$Message.error(data.message)
                            }

                        }).catch(err => {
                            console.log(err)
                        })

                },
                onCancel: () => {
                    // this.$Message.info('Clicked cancel')
                }
            })


        },
        // 批量删除url
        deleteUrl() {
            return this.preUrl + '/delete'
        },
        // 批量删除
        deleteAll() {
            if (this.multipleSelection.length === 0) {
                return this.$Message.error('请先选中需要删除的信息')
            }

            this.deleteItem(this.multipleSelection.toString())

        },
        // 多选
        selected(solution) {
            this.multipleSelection = []
            solution.forEach(item => {
                this.multipleSelection.push(item.id)
            })

        },
        // 删除单个
        deleteItem(id) {
            this.$Modal.confirm({
                title: '是否要删除选中',
                onOk: () => {
                    let url = this.deleteUrl()
                    this.$ajax.post(url, { id })
                        .then(res => {
                            if (!res.error_code) {
                                this.$Message.success(res.message)
                                this.getList()
                                if (this.getOptions) this.getOptions()
                            } else {
                                this.$Message.error(res.message)
                            }

                            this.hideLoading()
                        }).catch(err => {
                            this.hideLoading()
                        })
                },
                onCancel: () => {
                    // this.$Message.info('Clicked cancel')
                }
            })
        },
        // 分页
        changePage(index) {
            this.page.current = index
            this.getList()
        }

    }
}